package com.chaoxuan.uebt.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.adapter.OrderAdapter;
import com.chaoxuan.uebt.model.Order;
import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.CacheUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

public class OrderActivity extends BaseActivity implements View.OnClickListener,OnRefreshListener,OnLoadMoreListener,BaseQuickAdapter.OnItemClickListener {
    SmartRefreshLayout refreshLayout;
    OrderAdapter adapter;
    Button btn1,btn2,btn3,btn4;
    int page=0;
    int status=-1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        initTitle();
        btn1=(Button) findViewById(R.id.btn_status1);
        btn2=(Button) findViewById(R.id.btn_status2);
        btn3=(Button) findViewById(R.id.btn_status3);
        btn4=(Button) findViewById(R.id.btn_status4);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(linearLayoutManager);
        refreshLayout = (SmartRefreshLayout)findViewById(R.id.refreshLayout);

        adapter= new OrderAdapter();
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        adapter.setOnItemClickListener(this);
        refreshLayout.autoRefresh();
        //refreshLayout.setEnableLoadMore(false);
        btn1.setSelected(true);
    }

    @Override
    public void onClick(View v) {
        btn1.setSelected(false);
        btn2.setSelected(false);
        btn3.setSelected(false);
        btn4.setSelected(false);
        v.setSelected(true);
        switch (v.getId()){
            case R.id.btn_status1://全部
                status=-1;
                refreshLayout.autoRefresh();
                break;
            case R.id.btn_status2://0：待发货
                status=0;
                refreshLayout.autoRefresh();
                break;
            case R.id.btn_status3://1：待收货
                status=1;
                refreshLayout.autoRefresh();
                break;
            case R.id.btn_status4://2：已完成
                status=2;
                refreshLayout.autoRefresh();
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Order order=(Order) adapter.getData().get(position);
        if(order==null)return;
        Intent i=new Intent(this,OrderDetailActivity.class);
        i.putExtra("id",order.getId());
        startActivity(i);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        load(true,refreshLayout);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        load(false,refreshLayout);
    }

    private void load(final boolean refresh,final RefreshLayout refreshLayout){
        Log.d("----","load");
        User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        if(refresh)page=0;else page=adapter.getData().size();
        subscribe(RetrofitUtils.getAPIService().orderLists(page,status,user.getToken(),Const.DEVICE_TYPE), new BaseSubscriber<List<Order>>() {
            @Override
            public void onSuccess(List<Order> data) {
                if(refresh){
                    adapter.replaceData(data);
                    refreshLayout.finishRefresh();
                }else{
                    adapter.addData(data);
                    refreshLayout.finishLoadMore();
                }
            }
        });
    }
}
